package com.secbox.overlayutil;

import com.liferay.nativity.control.NativityControl;
import com.liferay.nativity.control.NativityControlUtil;
import com.liferay.nativity.modules.fileicon.FileIconControl;
import com.liferay.nativity.modules.fileicon.FileIconControlCallback;
import com.liferay.nativity.modules.fileicon.FileIconControlUtil;
import com.secbox.overlayutil.SecBoxOverlayUtil.StatusType;


import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.io.File;
import java.io.IOException;
import java.nio.file.attribute.DosFileAttributeView;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.logging.Level;

import org.omg.CORBA.PUBLIC_MEMBER;

public class SecBoxOverlayUtil {
	
	private static final int SYNCING = 1002;
	private static final int UPDATE = 1001;
	private static final int ERROR = 1003;
	
	private static Map<String, Boolean> haveRefreshed = new HashMap<String, Boolean>();

	public static ConcurrentHashMap<String, Integer> status = new ConcurrentHashMap<>();
	
	public static boolean refreshIcon(String rootFolder, String[] refreshIcons,
			final StatusType statusType) {
		
		String drive = rootFolder.substring(0, 1);
		boolean haveRefresh = haveRefreshed.containsKey(drive);
		
		boolean flag = false;

		NativityControl nativityControl = NativityControlUtil
				.getNativityControl();
		FileIconControlCallback fileIconControlBack1Callback = new FileIconControlCallback() {
			@Override
			public int getIconForFile(String path) {
				int sta = status.get(path);
				if (sta == 0) {
					return SYNCING;
				}
				return sta;
			}
		};
		
		switch (statusType) {
		case Syncing:
			for (int i = 0; i < refreshIcons.length; i++) {
				String refreshIcon = refreshIcons[i];
				status.put(refreshIcon, SYNCING);
			}
			break;
		case Uptodate:
			for (int i = 0; i < refreshIcons.length; i++) {
				String refreshIcon = refreshIcons[i];
				status.put(refreshIcon, UPDATE);
			}
			break;
		default:
			for (int i = 0; i < refreshIcons.length; i++) {
				String refreshIcon = refreshIcons[i];
				status.put(refreshIcon, ERROR);
			}
			break;
		}

		FileIconControl fileIconControl = FileIconControlUtil
				.getFileIconControl(nativityControl,
						fileIconControlBack1Callback);

//		if (!haveRefresh) {
			// setSystemFolder
			Path path = Paths.get(rootFolder);
			DosFileAttributeView dosView = Files.getFileAttributeView(path,
					DosFileAttributeView.class);
			try {
				dosView.setSystem(true);
			} catch (IOException e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			}
//		}

		flag = nativityControl.connect();
		
		if (!haveRefresh) {
			fileIconControl.enableFileIcons();	
//			nativityControl.setFilterFolder(rootFolder);
			nativityControl.setFilterFolder(drive);
			haveRefreshed.put(drive, true);
		}

//		nativityControl.setFilterFolder(rootFolder);
		fileIconControl.refreshIcons(refreshIcons);
		fileIconControl.refreshWindow(rootFolder);

		return flag;
	}
	
	public static void showUpdateIcon(String changeFile) {
		try {
			int lastIndex = changeFile.lastIndexOf('/');
			if (lastIndex == -1) {
				lastIndex = changeFile.lastIndexOf('\\');
			}
			String prefix = changeFile.substring(0, lastIndex);
			SecBoxOverlayUtil.refreshIcon(prefix, new String[] { changeFile }, StatusType.Uptodate);
			
			File file = new File(changeFile);
			if (file.exists() && file.isDirectory()) {
				String[] fileList = file.list();
				for (String fileStr : fileList) {
					showUpdateIcon(fileStr);
				}
			}
			
		} catch (Exception e) {
			System.out.println("show update fileIcon error->" + changeFile + ", " + e.getMessage());
		}
	}

	public static void showSyncIcon(String changeFile) {
		try {
			int lastIndex = changeFile.lastIndexOf('/');
			if (lastIndex == -1) {
				lastIndex = changeFile.lastIndexOf('\\');
			}
			String prefix = changeFile.substring(0, lastIndex);
			
			SecBoxOverlayUtil.refreshIcon(prefix, new String[] { changeFile }, StatusType.Syncing);
			
			File file = new File(changeFile);
			if (file.exists() && file.isDirectory()) {
				String[] fileList = file.list();
				for (String fileStr : fileList) {
					showSyncIcon(fileStr);
				}
			}
			
		} catch (Exception e) {
			System.out.println("show update fileIcon error->" + changeFile + ", " + e.getMessage());
		}
	}

	public enum StatusType {
		Syncing, Uptodate, Error,
	}
}
